﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 04.06.2014
 * Time: 15:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms.VisualStyles;
using Region.Utils;
namespace Region.Forms
{
  /// <summary>
  /// Description of FIOForm.
  /// </summary>
  public partial class FIOForm : Form
  {
    String m_ID;
    Engine m_engine;
    Links m_links;
    void read()
    {
      IEnumerable<Links> table = (from t in m_engine.bd_FIO.Links where t.ID==m_ID select t);
      if (table.Any())
      {
        m_links = table.First();
        FirstName.Text = m_links.FirstName;
        MiddleName.Text = m_links.MiddleName;
        LastName.Text = m_links.LastName;        
        ID.Text = m_links.ID;
        Username.Text = m_links.Username;
        FirstName.ReadOnly = true;
        MiddleName.ReadOnly = true;
        LastName.ReadOnly = true;
        buttonSave.Enabled = false;
        buttonPrint.Enabled = true;
        TypeData.Enabled = true;
        buttonAddData.Enabled = true;
        listData.Enabled = true;
        IEnumerable<Address> t2 = (from t in m_engine.bd_FIO.Address where t.LinksUID==m_links.LinksUID select t);
        foreach(Address address in t2)
          listData.Items.Add(new ListItemValue(address.ToString(),address));
        
      }
      else if (m_ID=="")
      {
        m_links = null;
        FirstName.ReadOnly = false;
        MiddleName.ReadOnly = false;
        LastName.ReadOnly = false;
        buttonSave.Enabled = true;
        buttonPrint.Enabled = false;
        TypeData.Enabled = false; 
        buttonAddData.Enabled = false; 
        listData.Enabled = false;        
      }
      else
      {
        MessageBox.Show("В журнале нет указанного идентификатора","Ошибка",MessageBoxButtons.OK,MessageBoxIcon.Stop);
        Close();
      }

    }
    void save()
    {
      if (m_links==null)
      {
        m_links = new Region.Links();
        m_links.FirstName = FirstName.Text;
        m_links.MiddleName = MiddleName.Text;
        m_links.LastName = LastName.Text;
        m_engine.bd_FIO.Links.InsertOnSubmit(m_links);
        m_engine.bd_FIO.SubmitChanges();
        m_ID = m_links.ID;

      }
      else
      {
        m_links.FirstName = FirstName.Text;
        m_links.MiddleName = MiddleName.Text;
        m_links.LastName = LastName.Text;
        m_engine.bd_FIO.SubmitChanges();       
      }
      read();
    }
    public FIOForm(Engine engine, String ID="")
    {
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      m_ID = ID;
      m_engine = engine;
      read();
      
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
    }
		void ButtonCloseClick(object sender, System.EventArgs e)
		{
		  Close();
		}
		void ButtonSaveClick(object sender, System.EventArgs e)
		{
		  save();
		}
    //private StreamReader streamToPrint;
    // The PrintPage event is raised for each page to be printed.
    private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
    {
      IEnumerable<Links> table = (from t in m_engine.bd_FIO.Links where t.ID==m_ID select t);
      m_links = table.First();
      Font printFontArial10 = new Font("Arial", 10);
      Font printFontArial20 = new Font("Arial", 20);
       int count = 0;
       float leftMargin = ev.MarginBounds.Left;
       float topMargin = ev.MarginBounds.Top;
       float linesPerPage = 0;
       float yPos =  topMargin;
       String line=null;
       ev.Graphics.DrawString ("Карточка обезличивания", printFontArial20, Brushes.Black, 
             leftMargin, yPos, new StringFormat());
       yPos += printFontArial20.GetHeight(ev.Graphics);

       ev.Graphics.DrawString ("Управление Роспотребнадзора по Хабаровскому краю", printFontArial10, Brushes.Black,
             leftMargin, yPos, new StringFormat());
       yPos += printFontArial10.GetHeight(ev.Graphics);

       ev.Graphics.DrawString (m_links.ID, printFontArial10, Brushes.Black,
             leftMargin, yPos, new StringFormat());
       yPos += printFontArial10.GetHeight(ev.Graphics);

       ev.Graphics.DrawString (m_links.LastName, printFontArial10, Brushes.Black,
             leftMargin, yPos, new StringFormat());
       yPos += printFontArial10.GetHeight(ev.Graphics);

       ev.Graphics.DrawString (m_links.FirstName, printFontArial10, Brushes.Black,
             leftMargin, yPos, new StringFormat());
       yPos += printFontArial10.GetHeight(ev.Graphics);

       ev.Graphics.DrawString (m_links.MiddleName, printFontArial10, Brushes.Black,
             leftMargin, yPos, new StringFormat());
       yPos += printFontArial10.GetHeight(ev.Graphics);
       
       IEnumerable<Address> t2 = (from t in m_engine.bd_FIO.Address where t.LinksUID==m_links.LinksUID select t);
       foreach(Address address in t2)
       {
         ev.Graphics.DrawString ("Адрес: " + address.ToString(), printFontArial10, Brushes.Black,
               leftMargin, yPos, new StringFormat());
         yPos += printFontArial10.GetHeight(ev.Graphics);
       }
    }		
		void ButtonPrintClick(object sender, System.EventArgs e)
		{
			try 
      {
        PrintDocument pd = new PrintDocument(); 
        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
        // Print the document.
        pd.Print();
      } 
      catch(Exception ex) 
      { 
          MessageBox.Show(ex.Message);
      }
		}
		void ButtonEditDataClick(object sender, System.EventArgs e)
		{
		  var item = listData.SelectedItem as ListItemValue;
		  if (item!=null)
		  {
		    var a = item.Value as Address;
		    if (a!=null)
		    {
		      var form = new Forms.EditAddress(m_engine,a);
		      form.ShowDialog();
		    }
		  }
		}
		void ButtonAddDataClick(object sender, System.EventArgs e)
		{
		  if (TypeData.Text=="Адрес" && m_links!=null)
		  {
			  Address a =  new Region.Address();
			  a.LinksUID = m_links.LinksUID;
  		  a.Region = "";
		    a.Raion = "";
    	  a.City = "";
    	  a.Settlement = "";
    	  a.Street = "";
    	  a.Home = "";
    	  a.Structure = "";
    	  a.Room = "";
        m_engine.bd_FIO.Address.InsertOnSubmit(a);
        m_engine.bd_FIO.SubmitChanges();
        listData.Items.Add(new ListItemValue(a.ToString(),a));;
        listData.SelectedItem = a;
        ButtonEditDataClick(sender, e);
		  }
		}
  }
}
